package com.yanqu.road.server.manager.activity.dragonboatrace;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.DragonBoatRaceBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author cwq
 * @Data 2022/5/5 19:01
 */
public class DragonBoatRaceConfigMgr {

    private static Logger logger = LogManager.getLogger(DragonBoatRaceConfigMgr.class.getName());

    private static Map<Integer, DragonBoatRaceConfig> configMap = new ConcurrentHashMap<>();

    public static synchronized boolean reloadConfig() {
        logger.info("start reload DragonBoatRace activity config  start");
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.DragonBoatRaceActivity.getValue());

        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo item : activityInfoMap.values()) {
            reloadConfig(item.getActivityId());
        }
        logger.info("start reload DragonBoatRace  activity config  end");

        return false;
    }

    public static synchronized void reloadConfig(int activityId) {
        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityId);
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityId);
        DragonBoatRaceConfig dragonBoatRaceConfig = new DragonBoatRaceConfig();
        dragonBoatRaceConfig.setPlaceConfigList(DragonBoatRaceBusiness.getDragonBoatRacePlaceConfigList(activityId));
        dragonBoatRaceConfig.setSpeedList(DragonBoatRaceBusiness.getDragonBoatRaceSpeedConfigList(activityId));
        dragonBoatRaceConfig.parseConfig(activityConfigMap);
        configMap.put(activityId, dragonBoatRaceConfig);
    }

    public static DragonBoatRaceConfig getConfigMap(int activityId) {
        return configMap.get(activityId);
    }
}
